Conditions | 108 |
Paths | 156 |
Total Lines | 188 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like proto.write often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*global Buffer*/ |
||
127 | proto.write = function (buffer) { |
||
128 | if (typeof buffer === "string") buffer = new Buffer(buffer); |
||
129 | var n; |
||
130 | for (var i = 0, l = buffer.length; i < l; i++) { |
||
131 | if (this.tState === START){ |
||
132 | n = buffer[i]; |
||
133 | this.offset++; |
||
134 | if(n === 0x7b){ this.onToken(LEFT_BRACE, "{"); // { |
||
135 | }else if(n === 0x7d){ this.onToken(RIGHT_BRACE, "}"); // } |
||
136 | }else if(n === 0x5b){ this.onToken(LEFT_BRACKET, "["); // [ |
||
137 | }else if(n === 0x5d){ this.onToken(RIGHT_BRACKET, "]"); // ] |
||
138 | }else if(n === 0x3a){ this.onToken(COLON, ":"); // : |
||
139 | }else if(n === 0x2c){ this.onToken(COMMA, ","); // , |
||
140 | }else if(n === 0x74){ this.tState = TRUE1; // t |
||
141 | }else if(n === 0x66){ this.tState = FALSE1; // f |
||
142 | }else if(n === 0x6e){ this.tState = NULL1; // n |
||
143 | }else if(n === 0x22){ // " |
||
144 | this.string = ""; |
||
145 | this.stringBufferOffset = 0; |
||
146 | this.tState = STRING1; |
||
147 | }else if(n === 0x2d){ this.string = "-"; this.tState = NUMBER1; // - |
||
148 | }else{ |
||
149 | if (n >= 0x30 && n < 0x40) { // 1-9 |
||
150 | this.string = String.fromCharCode(n); this.tState = NUMBER3; |
||
151 | } else if (n === 0x20 || n === 0x09 || n === 0x0a || n === 0x0d) { |
||
152 | // whitespace |
||
153 | } else { |
||
154 | return this.charError(buffer, i); |
||
155 | } |
||
156 | } |
||
157 | }else if (this.tState === STRING1){ // After open quote |
||
158 | n = buffer[i]; // get current byte from buffer |
||
159 | // check for carry over of a multi byte char split between data chunks |
||
160 | // & fill temp buffer it with start of this data chunk up to the boundary limit set in the last iteration |
||
161 | if (this.bytes_remaining > 0) { |
||
162 | for (var j = 0; j < this.bytes_remaining; j++) { |
||
163 | this.temp_buffs[this.bytes_in_sequence][this.bytes_in_sequence - this.bytes_remaining + j] = buffer[j]; |
||
164 | } |
||
165 | |||
166 | this.appendStringBuf(this.temp_buffs[this.bytes_in_sequence]); |
||
167 | this.bytes_in_sequence = this.bytes_remaining = 0; |
||
168 | i = i + j - 1; |
||
169 | } else if (this.bytes_remaining === 0 && n >= 128) { // else if no remainder bytes carried over, parse multi byte (>=128) chars one at a time |
||
170 | if (n <= 193 || n > 244) { |
||
171 | return this.onError(new Error("Invalid UTF-8 character at position " + i + " in state " + Parser.toknam(this.tState))); |
||
172 | } |
||
173 | if ((n >= 194) && (n <= 223)) this.bytes_in_sequence = 2; |
||
174 | if ((n >= 224) && (n <= 239)) this.bytes_in_sequence = 3; |
||
175 | if ((n >= 240) && (n <= 244)) this.bytes_in_sequence = 4; |
||
176 | if ((this.bytes_in_sequence + i) > buffer.length) { // if bytes needed to complete char fall outside buffer length, we have a boundary split |
||
177 | for (var k = 0; k <= (buffer.length - 1 - i); k++) { |
||
178 | this.temp_buffs[this.bytes_in_sequence][k] = buffer[i + k]; // fill temp buffer of correct size with bytes available in this chunk |
||
179 | } |
||
180 | this.bytes_remaining = (i + this.bytes_in_sequence) - buffer.length; |
||
181 | i = buffer.length - 1; |
||
182 | } else { |
||
183 | this.appendStringBuf(buffer, i, i + this.bytes_in_sequence); |
||
184 | i = i + this.bytes_in_sequence - 1; |
||
185 | } |
||
186 | } else if (n === 0x22) { |
||
187 | this.tState = START; |
||
188 | this.string += this.stringBuffer.toString('utf8', 0, this.stringBufferOffset); |
||
189 | this.stringBufferOffset = 0; |
||
190 | this.onToken(STRING, this.string); |
||
191 | this.offset += Buffer.byteLength(this.string, 'utf8') + 1; |
||
192 | this.string = undefined; |
||
193 | } |
||
194 | else if (n === 0x5c) { |
||
195 | this.tState = STRING2; |
||
196 | } |
||
197 | else if (n >= 0x20) { this.appendStringChar(n); } |
||
198 | else { |
||
199 | return this.charError(buffer, i); |
||
200 | } |
||
201 | }else if (this.tState === STRING2){ // After backslash |
||
202 | n = buffer[i]; |
||
203 | if(n === 0x22){ this.appendStringChar(n); this.tState = STRING1; |
||
204 | }else if(n === 0x5c){ this.appendStringChar(BACK_SLASH); this.tState = STRING1; |
||
205 | }else if(n === 0x2f){ this.appendStringChar(FORWARD_SLASH); this.tState = STRING1; |
||
206 | }else if(n === 0x62){ this.appendStringChar(BACKSPACE); this.tState = STRING1; |
||
207 | }else if(n === 0x66){ this.appendStringChar(FORM_FEED); this.tState = STRING1; |
||
208 | }else if(n === 0x6e){ this.appendStringChar(NEWLINE); this.tState = STRING1; |
||
209 | }else if(n === 0x72){ this.appendStringChar(CARRIAGE_RETURN); this.tState = STRING1; |
||
210 | }else if(n === 0x74){ this.appendStringChar(TAB); this.tState = STRING1; |
||
211 | }else if(n === 0x75){ this.unicode = ""; this.tState = STRING3; |
||
212 | }else{ |
||
213 | return this.charError(buffer, i); |
||
214 | } |
||
215 | }else if (this.tState === STRING3 || this.tState === STRING4 || this.tState === STRING5 || this.tState === STRING6){ // unicode hex codes |
||
216 | n = buffer[i]; |
||
217 | // 0-9 A-F a-f |
||
218 | if ((n >= 0x30 && n < 0x40) || (n > 0x40 && n <= 0x46) || (n > 0x60 && n <= 0x66)) { |
||
219 | this.unicode += String.fromCharCode(n); |
||
220 | if (this.tState++ === STRING6) { |
||
221 | var intVal = parseInt(this.unicode, 16); |
||
222 | this.unicode = undefined; |
||
223 | if (this.highSurrogate !== undefined && intVal >= 0xDC00 && intVal < (0xDFFF + 1)) { //<56320,57343> - lowSurrogate |
||
224 | this.appendStringBuf(new Buffer(String.fromCharCode(this.highSurrogate, intVal))); |
||
225 | this.highSurrogate = undefined; |
||
226 | } else if (this.highSurrogate === undefined && intVal >= 0xD800 && intVal < (0xDBFF + 1)) { //<55296,56319> - highSurrogate |
||
227 | this.highSurrogate = intVal; |
||
228 | } else { |
||
229 | if (this.highSurrogate !== undefined) { |
||
230 | this.appendStringBuf(new Buffer(String.fromCharCode(this.highSurrogate))); |
||
231 | this.highSurrogate = undefined; |
||
232 | } |
||
233 | this.appendStringBuf(new Buffer(String.fromCharCode(intVal))); |
||
234 | } |
||
235 | this.tState = STRING1; |
||
236 | } |
||
237 | } else { |
||
238 | return this.charError(buffer, i); |
||
239 | } |
||
240 | } else if (this.tState === NUMBER1 || this.tState === NUMBER3) { |
||
241 | n = buffer[i]; |
||
242 | |||
243 | switch (n) { |
||
244 | case 0x30: // 0 |
||
245 | case 0x31: // 1 |
||
246 | case 0x32: // 2 |
||
247 | case 0x33: // 3 |
||
248 | case 0x34: // 4 |
||
249 | case 0x35: // 5 |
||
250 | case 0x36: // 6 |
||
251 | case 0x37: // 7 |
||
252 | case 0x38: // 8 |
||
253 | case 0x39: // 9 |
||
254 | case 0x2e: // . |
||
255 | case 0x65: // e |
||
256 | case 0x45: // E |
||
257 | case 0x2b: // + |
||
258 | case 0x2d: // - |
||
259 | this.string += String.fromCharCode(n); |
||
260 | this.tState = NUMBER3; |
||
261 | break; |
||
262 | default: |
||
263 | this.tState = START; |
||
264 | var result = Number(this.string); |
||
265 | |||
266 | if (isNaN(result)){ |
||
267 | return this.charError(buffer, i); |
||
268 | } |
||
269 | |||
270 | if ((this.string.match(/[0-9]+/) == this.string) && (result.toString() != this.string)) { |
||
271 | // Long string of digits which is an ID string and not valid and/or safe JavaScript integer Number |
||
272 | this.onToken(STRING, this.string); |
||
273 | } else { |
||
274 | this.onToken(NUMBER, result); |
||
275 | } |
||
276 | |||
277 | this.offset += this.string.length - 1; |
||
278 | this.string = undefined; |
||
279 | i--; |
||
280 | break; |
||
281 | } |
||
282 | }else if (this.tState === TRUE1){ // r |
||
283 | if (buffer[i] === 0x72) { this.tState = TRUE2; } |
||
284 | else { return this.charError(buffer, i); } |
||
285 | }else if (this.tState === TRUE2){ // u |
||
286 | if (buffer[i] === 0x75) { this.tState = TRUE3; } |
||
287 | else { return this.charError(buffer, i); } |
||
288 | }else if (this.tState === TRUE3){ // e |
||
289 | if (buffer[i] === 0x65) { this.tState = START; this.onToken(TRUE, true); this.offset+= 3; } |
||
290 | else { return this.charError(buffer, i); } |
||
291 | }else if (this.tState === FALSE1){ // a |
||
292 | if (buffer[i] === 0x61) { this.tState = FALSE2; } |
||
293 | else { return this.charError(buffer, i); } |
||
294 | }else if (this.tState === FALSE2){ // l |
||
295 | if (buffer[i] === 0x6c) { this.tState = FALSE3; } |
||
296 | else { return this.charError(buffer, i); } |
||
297 | }else if (this.tState === FALSE3){ // s |
||
298 | if (buffer[i] === 0x73) { this.tState = FALSE4; } |
||
299 | else { return this.charError(buffer, i); } |
||
300 | }else if (this.tState === FALSE4){ // e |
||
301 | if (buffer[i] === 0x65) { this.tState = START; this.onToken(FALSE, false); this.offset+= 4; } |
||
302 | else { return this.charError(buffer, i); } |
||
303 | }else if (this.tState === NULL1){ // u |
||
304 | if (buffer[i] === 0x75) { this.tState = NULL2; } |
||
305 | else { return this.charError(buffer, i); } |
||
306 | }else if (this.tState === NULL2){ // l |
||
307 | if (buffer[i] === 0x6c) { this.tState = NULL3; } |
||
308 | else { return this.charError(buffer, i); } |
||
309 | }else if (this.tState === NULL3){ // l |
||
310 | if (buffer[i] === 0x6c) { this.tState = START; this.onToken(NULL, null); this.offset += 3; } |
||
311 | else { return this.charError(buffer, i); } |
||
312 | } |
||
313 | } |
||
314 | }; |
||
315 | proto.onToken = function (token, value) { |
||
414 |